#include "mainwidget.h"
#include "./ui_mainwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "sessionfriendarea.h"
#include "debug.h"
MainWidget* MainWidget::instance = nullptr;
MainWidget* MainWidget::getInstance(){
    if(instance == nullptr){
        //此处不传入参数，以桌面父窗口
        // 由于此处的窗口是整个程序的主窗口，父窗口设定为桌面，本身就是常规设定
        instance = new MainWidget();
    }

    return instance;
}


MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("我的聊天室");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));

    initMainWindow();
    initLeftWindow();
    initMidWindow();
    initRightWindow();

    //初始化信号槽
    initSignalSlot();
}

MainWidget::~MainWidget()
{
    delete ui;
}


void MainWidget::initMainWindow(){
    QHBoxLayout* layout = new QHBoxLayout();

    //layout内部元素之间的间隔
    layout->setSpacing(0);
    //里面元素距离四个边界的距离
    layout->setContentsMargins(0, 0, 0, 0);

    this->setLayout(layout);    //添加到整体的页面
    windowLeft = new QWidget();
    windowMid = new QWidget();
    windowRight = new QWidget();

    windowLeft->setFixedWidth(70);
    windowMid->setFixedWidth(310);
    windowRight->setMinimumWidth(700);

    windowLeft->setStyleSheet("QWidget {background-color: rgb(46, 46, 46);}");
    windowMid->setStyleSheet("QWidget {background-color: rgb(247, 247, 247);}");
    windowRight->setStyleSheet("QWidget {background-color: rgb(225, 225, 225);}");

    //添加到主界面
    layout->addWidget(windowLeft);
    layout->addWidget(windowMid);
    layout->addWidget(windowRight);


}

void MainWidget::initLeftWindow(){

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(20);   //设置空间20像素点
    layout->setContentsMargins(QMargins(0, 50 , 0, 0));
    windowLeft->setLayout(layout);

    //添加用户头像
    userAvatar = new QPushButton();
    userAvatar->setFixedSize(45, 45);
    userAvatar->setIconSize(QSize(45, 45));   //图片尺寸
    userAvatar->setIcon(QIcon(":/resource/image/defaultAvatar.jpg"));
    userAvatar->setStyleSheet("QPushButton { background-color: transparent; }");
    layout->addWidget(userAvatar, 1, Qt::AlignTop | Qt::AlignHCenter);   //水平居中，垂直方向上靠上对齐

    //添加会话标签按钮
    sessionTabBtn = new QPushButton();
    sessionTabBtn->setFixedSize(45,45);
    sessionTabBtn->setIconSize(QSize(30, 30));
    sessionTabBtn->setIcon(QIcon(":/resource/image/session_active.png"));
    sessionTabBtn->setStyleSheet("QPushButton {background-color: transparent;}");
    layout->addWidget(sessionTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    //添加好友标签按钮
    friendTabBtn = new QPushButton();
    friendTabBtn->setFixedSize(45,45);
    friendTabBtn->setIconSize(QSize(30, 30));
    friendTabBtn->setIcon(QIcon(":/resource/image/friend_activate.png"));
    friendTabBtn->setStyleSheet("QPushButton {background-color: transparent;}");
    layout->addWidget(friendTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    //添加好友申请标签按钮
    applyTabBtn = new QPushButton();
    applyTabBtn->setFixedSize(45,45);
    applyTabBtn->setIconSize(QSize(30, 30));
    applyTabBtn->setIcon(QIcon(":/resource/image/apply_active.png"));
    applyTabBtn->setStyleSheet("QPushButton { background-color: transparent;}");
    layout->addWidget(applyTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    layout->addStretch(20);


    //连接信号槽
}
void MainWidget::initMidWindow(){
   //添加到整体的页面,既有水平页面又有垂直页面
    QGridLayout* layout = new QGridLayout();
    //距离上方又20px的距离，另外三个方向不要边距
    layout->setContentsMargins(0, 20, 0, 0);
    layout->setHorizontalSpacing(0);   //元素之间的距离
    layout->setVerticalSpacing(10);
    windowMid->setLayout(layout);

    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(30);
    searchEdit->setPlaceholderText("搜索");  //搜索框中的文字
    searchEdit->setStyleSheet("QLineEdit {border-radius:5px; background-color:rgb(226, 226, 226);color: black;padding-left:5px;}");

    addFriendBtn = new QPushButton();
    addFriendBtn->setFixedSize(30, 30);
    addFriendBtn->setIcon(QIcon(":/resource/image/cros.png"));
    QString style = "QPushButton {border-radius:5px; background-color:rgb(226, 226, 226);}";
    style+="QPushButton:pressed {background-color: rgb(240, 240, 240)}";   //点击更换颜色
    addFriendBtn->setStyleSheet(style);

    SessionFriendArea* sessionFriendArea = new SessionFriendArea();

    //为了更灵活的控制边距，之影响第一行，不影响其他行。
    //创建空白的区域Widget填充到布局管理器中
    QWidget* spacer1 = new QWidget();
    spacer1->setFixedWidth(10);
    QWidget* spacer2 = new QWidget();
    spacer2->setFixedWidth(10);
    QWidget* spacer3 = new QWidget();
    spacer3->setFixedWidth(10);

    layout->addWidget(spacer1, 0, 0);
    layout->addWidget(searchEdit, 0, 1);  //第0行第0列
    layout->addWidget(spacer2, 0, 2);
    layout->addWidget(addFriendBtn, 0, 3);
    layout->addWidget(spacer3, 0, 4);

    //上边有五列，这个用1，0会导致只有第一列   ,所以再加上1,5
    layout->addWidget(sessionFriendArea, 1, 0, 1, 5);


    // SessionFriendArea(nullptr);

}
void MainWidget::initRightWindow(){

    //1. 创建右侧窗口的布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setAlignment(Qt::AlignTop);
    windowRight->setLayout(vlayout);

    //2. 创建右测上方的标题栏
    QWidget* titleWidget = new QWidget();
    titleWidget->setFixedHeight(62);
    titleWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    titleWidget->setObjectName("titleWidget");
    titleWidget->setStyleSheet("#titleWidget {border-bottom:1px solid rgb(230, 230, 230);border-left:1px solid rgb(230, 230, 230);}");
    vlayout->addWidget(titleWidget);

    //3. 给标题栏里面添加label 和一个按钮,要搞一个水平的布局管理器
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(10, 0, 10, 0);  //标题的按钮左右间距有点距离
    titleWidget->setLayout(hlayout);

    QLabel* sessionTitleLabel = new QLabel();
    sessionTitleLabel->setStyleSheet("QLabel {font-size:22px;color:black;border-bottom:1px solid rgb(230, 230, 230);}");
#if TEST_UI
    //实际这里是从服务器获取的数据
    sessionTitleLabel->setText("标题");
#endif
    hlayout->addWidget(sessionTitleLabel);
    QPushButton* extraBtn = new QPushButton();
    extraBtn->setFixedSize(30, 30);
    extraBtn->setIconSize(QSize(30, 30));
    extraBtn->setIcon(QIcon(":/resource/image/more.png"));
    extraBtn->setStyleSheet("QPushButton {border:none;background-color:rgb(245, 245, 245);} QPushButton:pressed{background-color:rgb(220, 220, 220);}");
    hlayout->addWidget(extraBtn);


    //4. 添加消息展示区
    messageShowArea = new MessageShowArea();
    vlayout->addWidget(messageShowArea);
    //4. 添加消息消息编辑区
    messageEditArea = new MessageEditArea();
    vlayout->addWidget(messageEditArea, 0, Qt::AlignBottom);  //确保消息编辑区，处于窗口下方
}
void MainWidget::initSignalSlot(){
    ////////////////////////////////////////
    /// 连接信号槽
    ////////////////////////////////////////
    connect(sessionTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToSession);
    connect(friendTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToFriend);
    connect(applyTabBtn, &QPushButton::clicked, this, &MainWidget::switchTabToApply);

}

void MainWidget::switchTabToSession(){
    //1. 记录当前切换到了哪个标签页
    activeTab = SESSION_LIST;
    //2. 调整图标显示,把active换成inactive
    sessionTabBtn->setIcon(QIcon(":/resource/image/session_active.png"));
    friendTabBtn->setIcon(QIcon(":/resource/image/friend_inactivate.png"));
    applyTabBtn->setIcon(QIcon(":/resource/image/apply_inactive.png"));

    //3. 在中间加载会话列表示数据
    this->loadSessionList();

}

void MainWidget::switchTabToFriend(){
    activeTab = FRIEND_LIST;
    //2. 调整图标显示,把active换成inactive
    sessionTabBtn->setIcon(QIcon(":/resource/image/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/resource/image/friend_activate.png"));
    applyTabBtn->setIcon(QIcon(":/resource/image/apply_inactive.png"));
    this->loadFriendList();
}

void MainWidget::switchTabToApply(){
    activeTab = APPLY_LIST;
    //2. 调整图标显示,把active换成inactive
    sessionTabBtn->setIcon(QIcon(":/resource/image/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/resource/image/friend_inactivate.png"));
    applyTabBtn->setIcon(QIcon(":/resource/image/apply_active.png"));
    this->loadApplyList();
}

void MainWidget::loadSessionList(){

}

void MainWidget::loadFriendList(){

}

void MainWidget::loadApplyList(){

}


